import request from "@/utils/request";
import JsZip from "jszip";
import "file-saver";

// 下载文件列表
export async function download(data) {
  let { urlList, zipName } = data;
  let promiseList = [];
  for (let i = 0; i < urlList.length; i++) {
    let req = request({
      url: urlList[i],
      method: "get",
      responseType: "blob",
    });
    promiseList.push(req);
  }
  let result = await Promise.all(promiseList);
  let jsZip = new JsZip();
  for (let i = 0; i < result.length; i++) {
    jsZip.file(`${zipName}_${i + 1}.png`, result[i], { binary: true });
  }
  jsZip.generateAsync({ type: "blob" }).then((content) => {
    saveAs(content, `${zipName}.zip`);
  });
}
